<template>
  <div class="page">
    <div class="s-search p20">
      <el-form
        :model="alone.searchForm.data"
        status-icon
        ref="ruleForm"
        :inline="true"
        class="demo-ruleForm"
      >
        <el-form-item>
          <el-input
            placeholder="请输入主播昵称"
            v-model="alone.searchForm.data.nickName"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            placeholder="请输入招募名称"
            v-model="alone.searchForm.data.recruitName"
          >
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-input
            placeholder="请输入渠道昵称"
            v-model="alone.searchForm.data.channelName"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-cascader
            placeholder="请选择部门"
            v-model="alone.searchForm.data.departmentId"
            :props="orgCascaderCp.config.props"
            :options="orgSupAjax.data"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="alone.searchForm.data.createTimeStart"
            align="right"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            placeholder="选择提交开始日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="alone.searchForm.data.createTimeEnd"
            align="right"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            placeholder="选择提交结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="rSubmitSearchForm">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="c-main p20">
      <div class="c-aside clearfix">
        <el-button
          type="primary"
          class="fr btn"
          @click="rDownloadClueActor"
          v-hasAuth:seestar_clueActorListExport
          >导出主播线索</el-button
        >
        <!-- <el-button
          type="primary"
          class="fr btn"
          @click="rToUrl"
          v-hasAuth:actor_seestarCreate
          >新增</el-button
        > -->
      </div>
      <div class="c-section">
        <div class="c-body">
          <el-table :data="pageAjax.data" stripe style="width: 100%">
            <el-table-column prop="nickName" label="昵称">
              <template slot-scope="scope">
                <div class="">
                  {{ scope.row.nickName }}
                  <span v-if="scope.row.hasLiveExperience" class="text-warning"
                    >(有直播经验)</span
                  >
                  <span v-else>(无直播经验)</span>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="微信号">
              <template slot-scope="scope">
                <div class="">
                  {{ scope.row.wxNo || "无" }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="联系电话">
              <template slot-scope="scope">
                <div class="">
                  {{ scope.row.contractPhone || "无" }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="想获得报价平台(意向平台)">
              <template slot-scope="scope">
                <div class="">
                  {{ scope.row.intentionPlatform || "无" }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="渠道名称">
              <template slot-scope="scope">
                <div class="">
                  {{ scope.row.channelName || "无" }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="渠道类别">
              <template slot-scope="scope">
                <div class="">
                  {{ scope.row.type == "scout" ? "星探" : "联营" || "无" }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="渠道所属">
              <template slot-scope="scope">
                {{
                  scope.row.creator
                    ? scope.row.creator.departmentName +
                      "-" +
                      scope.row.creator.nickName +
                      "(" +
                      scope.row.creator.realName +
                      ")"
                    : "无"
                }}
              </template>
            </el-table-column>
            <el-table-column label="提交页类别">
              <template slot-scope="scope">
                {{ tranlate.submitPageType[scope.row.subType] || "无" }}
              </template>
            </el-table-column>
            <el-table-column label="提交时间">
              <template slot-scope="scope">
                <div class="">
                  {{ scope.row.createTime || "无" }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  size="small"
                  v-hasAuth:seestar_channelClueDetail
                  @click="navToDetail(scope.row.anchorId)"
                  >详情</el-button
                >
                <el-button
                  type="text"
                  size="small"
                  v-hasAuth:seestar_channelClueEdit
                  @click="navToEdit(scope.row.anchorId)"
                  >修改</el-button
                >
                <el-button
                  type="text"
                  size="small"
                  v-hasAuth:seestar_delete
                  @click="rDeleteActor(scope.row.anchorId)"
                  >删除</el-button
                >
                <el-button
                  type="text"
                  size="small"
                  v-hasAuth:seestar_actorClueStatusChange
                  @click="rChangeStatus(scope.row.anchorId)"
                  >变更为意向主播</el-button
                >
                <span
                  v-hasAuth:seestar_channelClueDetail-seestar_channelClueEdit-seestar_actorClueStatusChange-seestar_delete.invert
                  >—</span
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="c-footer">
          <div class="c-pagination">
            <el-pagination
              layout="prev, pager, next"
              @current-change="rPagingChange"
              @prev-click="rPagingChange"
              @next-click="rPagingChange"
              :current-page="pageAjax.paging.current"
              :total="pageAjax.paging.total"
            >
            </el-pagination>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {
  getClueActorList,
  deleteActor,
  changeActorStatus,
  downloadClueActor
} from "@/api/actor.js";
import { getH5PageTypeList } from "@/api/channel.js";
import { mixin } from "@/components/mixin/getSalaryStr.js";
import { getOffspringnTree } from "@/assets/js/util.js";
import _ from "lodash";

export default {
  name: "seestar_channelClueList",
  mixins: [mixin],
  data: () => ({
    pageAjax: {
      btnDisabled: false,
      btnLoading: false,
      loading: false,
      lock: false,
      paging: {
        pages: 1, // 总页数
        current: 0, // 当前页
        total: 1, // 总数量
        pageSize: 10 // 每页数量
      },
      data: []
    },
    h5PageTypeListSupAjax: {
      data: []
    },
    orgSupAjax: {
      data: []
    },
    alone: {
      searchForm: {
        loading: false,
        data: {
          nickName: "",
          recruitName: "",
          channelName: "",
          createTimeStart: "",
          createTimeEnd: "",
          departmentId: ""
        }
      },
      signStatus: "POTENTIAL"
    },
    tranlate: {
      submitPageType: {}
    },
    orgCascaderCp: {
      config: {
        props: {
          value: "id",
          label: "name",
          checkStrictly: true,
          emitPath: false
        }
      }
    }
  }),
  created() {
    let _that = this;
    _that.getH5PageTypeList().then(() => {
      _that.getPageData();
    });
    _that.getOrgData();
  },
  methods: {
    rSubmitSearchForm() {
      let _that = this;
      if (_that.pageAjax.loading) {
        return;
      }

      _that.pageAjax.lock = false;
      _that.pageAjax.paging.current = 0;
      _that.getPageData();
    },
    rDownloadClueActor() {
      let _that = this;
      if (!_that.hasAuth(["seestar_clueActorListExport"])) {
        return;
      }

      let searchForm = _that.alone.searchForm;

      let loadingHandle = _that.$loading({
        text: "下载中。。。",
        lock: true
      });

      downloadClueActor({ params: { ...searchForm.data } })
        .then(resp => {
          let respData = resp.data || {};

          //if (respData.code === _that.STATUS.success[0]) {
          var blob = new Blob([respData]);
          var downloadElement = document.createElement("a");
          var href = window.URL.createObjectURL(blob); //创建下载的链接
          downloadElement.href = href;
          downloadElement.download = "主播线索数据导出.csv"; //下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); //点击下载
          document.body.removeChild(downloadElement); //下载完成移除元素
          window.URL.revokeObjectURL(href); //释放掉blob对象
          // } else {
          //   _that.$message.error(respData.msg || "查询失败");
          // }
        })
        .finally(() => {
          loadingHandle.close();
        });
    },
    rPagingChange(e) {
      let _that = this;
      _that.pageAjax.paging.current = e - 1;
      _that.getPageData();
    },
    rChangeStatus(anchorId) {
      let _that = this;
      let loadingHandle = _that.$loading({
        text: "修改中。。。",
        lock: true
      });
      changeActorStatus({ anchorId: anchorId, signStatus: "POTENTIAL" })
        .then(resp => {
          let respData = resp.data || {};

          if (respData.code === _that.STATUS.success[0]) {
            _that.$message.success(respData.msg || "修改成功");
            _that.pageAjax.paging.current =
              _that.pageAjax.paging.current > 0
                ? _that.pageAjax.paging.current - 1
                : 0;
            _that.getPageData();
          } else {
            _that.$message.error(respData.msg || "修改失败");
          }
        })
        .finally(() => {
          loadingHandle.close();
        });
    },

    rDeleteActor(id) {
      let _that = this;
      this.$confirm("你确定要删除此数据?", "删除提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteActor({ anchorId: id }).then(resp => {
            let respData = resp.data || {};

            if (respData.code === _that.STATUS.success[0]) {
              _that.$message.success(respData.msg || "删除成功");
              _that.pageAjax.paging.current =
                _that.pageAjax.paging.current > 0
                  ? _that.pageAjax.paging.current - 1
                  : 0;
              _that.getPageData();
            } else {
              _that.$message.error(respData.msg || "删除失败");
            }
          });
        })
        .catch(() => {});
    },
    getPageData() {
      let _that = this;
      let searchForm = _.cloneDeep(_that.alone.searchForm);
      let pageAjax = _that.pageAjax;

      if (pageAjax.loading || pageAjax.lock) {
        return;
      }

      let data = { ...searchForm.data };
      if (_that.$route.query.channelId) {
        data.channelId = _that.$route.query.channelId;
      }
      data.pageNo = _that.pageAjax.paging.current + 1;
      _that.pageAjax.loading = true;
      getClueActorList({ params: data })
        .then(resp => {
          let respData = resp.data || {};

          if (respData.code === _that.STATUS.success[0]) {
            _that.pageAjax.data = respData.data.list;
            _that.pageAjax.paging = {
              pages: respData.data.pagination.pages,
              current: respData.data.pagination.current,
              total: respData.data.pagination.total,
              pageSize: respData.data.pagination.pageSize
            };
          } else {
            _that.$message.error(respData.msg || "获取失败");
          }
        })
        .finally(() => {
          _that.pageAjax.loading = false;
        });
    },
    // 获取h5页面种类
    getH5PageTypeList() {
      let _that = this;
      return getH5PageTypeList()
        .then(resp => {
          let respData = resp.data || {};
          if (respData.code === _that.STATUS.success[0]) {
            let respResult = respData.data || [];
            let tranlatePageType = {};
            respResult.forEach(item => {
              // item.value = item.value.toLocaleLowerCase();
              tranlatePageType[item.value] = item.name;
            });
            _that.h5PageTypeListSupAjax.data = respResult;
            _that.tranlate.submitPageType = tranlatePageType;
            //}
          } else {
            _that.$message.error(respData.msg || "获取失败");
          }
        })
        .finally();
    },
    getOrgData() {
      let _that = this;
      _that.$store.dispatch("getDepartmentList").then(() => {
        let arr = _.cloneDeep(_that.$store.state.departmentList) || [];
        _that.orgSupAjax.data = getOffspringnTree(_.cloneDeep(arr), 0);
      });
    },
    rToUrl() {
      this.$router.push({ name: "seestar_create" });
    },
    navToEdit(id) {
      this.$router.push({
        name: "seestar_channelClueEdit",
        query: {
          anchorId: id
        }
      });
    },
    navToDetail(id) {
      this.$router.push({
        name: "seestar_channelClueDetail",
        query: {
          anchorId: id
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.page {
  .s-search {
    border-bottom: 1px solid #eee;
  }
  .c-aside {
    .btn {
      margin-right: 20px;
    }
    .diy-uploadBtn {
      margin-right: 20px;
    }
  }
  .c-main {
    margin-top: 20px;
    .c-avatar {
      display: block;
      width: 100px;
      height: 100px;
      border-radius: 50%;
    }
  }
  .c-pagination {
    margin-top: 20px;
    text-align: center;
  }
  .f-record {
    overflow: hidden;
    position: absolute;
    width: 100%;
    left: 0;
    top: 50px;
    bottom: 0;
    .c-content {
      height: 100%;
      padding-bottom: 20px;
      overflow: auto;
    }
  }

  .f-agent {
    overflow: hidden;
    position: absolute;
    width: 100%;
    left: 0;
    top: 0;
    bottom: 0;
    .c-content {
      height: 100%;
      padding-bottom: 20px;
      padding-top: 60px;
      overflow: auto;
    }
    .c-avatar {
      width: 80px;
      height: 80px;
    }
  }
}
</style>
